package com.ultron.app.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ultron.app.component.excel.EasyExcelUtil;
import com.ultron.app.component.excel.MyReadListener;
import com.ultron.app.component.excel.ReadBack;
import com.ultron.app.entity.system.Result;
import com.ultron.app.entity.test.FinanceChannelExcelBO;
import com.ultron.app.entity.test.FinanceQAAutoTestBO;
import com.ultron.app.entity.test.FinanceQAExcelBO;
import com.ultron.app.entity.test.FinanceQAExcelBO2;
import com.ultron.app.service.test.TestService;
import com.ultron.app.util.DateUtils;
import com.ultron.app.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 标题：测试(控制层)
 * 说明：测试(控制层),包括所有对外前端API接口
 * 时间：2023-9-4
 * 作者：admin
 */
@RestController
@CrossOrigin
@RequestMapping("/api/ultron/test")
@Api(value = "TestController", tags = "测试API")
public class TestController {

    @Autowired
    private TestService testService;


    @PostMapping("/qaAutoTest")
    @ApiOperation("QAz自动化测试接口API")
    public Result<Boolean> qaAutoTest(@RequestParam("file") MultipartFile req,HttpServletResponse response) throws IOException {
        boolean rsp = testService.qaAutoTest(req,response);
        return Result.success(rsp);
    }


    @GetMapping("/exportQaAutoExcelModule")
    @ApiOperation("导出QA自动化测试Excel模板API")
    @ResponseBody
    public void exportQaAutoExcelModule(HttpServletResponse response) throws IOException {
        List<FinanceQAAutoTestBO> rsp = new ArrayList<>();
        FinanceQAAutoTestBO example = new FinanceQAAutoTestBO().buildExportModule();
        rsp.add(example);
        ResponseUtil.setExcelResponse(response, "QA自动化测试结果"+ DateUtils.getCurrentDateStrYYYYMMDDHHMMSS3());
        EasyExcelUtil.getExcelWriterBuilder(response.getOutputStream(), FinanceQAAutoTestBO.class).sheet("sheet1").doWrite(rsp);
    }



    @GetMapping("/exportExcelModule")
    @ApiOperation("导出Excel模板API")
    @ResponseBody
    public void exportExcelModule(HttpServletResponse response) throws IOException {
        List<FinanceQAExcelBO> rsp = new ArrayList<>();
        FinanceQAExcelBO example = new FinanceQAExcelBO().buildExportModule();
        rsp.add(example);
        String name = "Excel模板";
        ResponseUtil.setExcelResponse(response, name);
        EasyExcelUtil.getExcelWriterBuilder(response.getOutputStream(), FinanceQAExcelBO.class).sheet("sheet").doWrite(rsp);
    }

    @GetMapping("/exportExcelModule2")
    @ApiOperation("导出Excel模板API")
    @ResponseBody
    public void exportExcelModule2(HttpServletResponse response) throws IOException {
        List<FinanceQAExcelBO2> rsp = new ArrayList<>();
        FinanceQAExcelBO2 example = new FinanceQAExcelBO2().buildExportModule();
        rsp.add(example);
        String name = "Excel模板";
        ResponseUtil.setExcelResponse(response, name);
        EasyExcelUtil.getExcelWriterBuilder(response.getOutputStream(), FinanceQAExcelBO2.class).sheet("sheet").doWrite(rsp);
    }

    @PostMapping("/analysisExcel")
    @ApiOperation("解析ExcelAPI")
    @ResponseBody
    public void analysisExcel() throws IOException {
        String excelPath = "D:\\SVN\\longheqiang\\财务板块\\财务共享AI\\财务AI报销问答机器人\\Excel模板20250319152601.xlsx";
        FileInputStream fileInputStream = new FileInputStream(excelPath);
        List<FinanceChannelExcelBO> excelList = new ArrayList<>();
        EasyExcel.read(fileInputStream, FinanceChannelExcelBO.class, new AnalysisEventListener<FinanceChannelExcelBO>() {
            @Override
            public void invoke(FinanceChannelExcelBO data, AnalysisContext context) {
                excelList.add(data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                // 可在此处理读取完成后的逻辑

            }
        }).sheet("sheet").doRead();
        // 遍历输出
        for (FinanceChannelExcelBO bo : excelList) {
            System.out.println("序号：" + bo.getXh());
            System.out.println("预算科目：" + bo.getYskm());
            System.out.println("业务场景：" + bo.getYwcj());
            System.out.println("业务场景解释：" + bo.getYwcjjs());
            System.out.println("费用类型："+bo.getBzy()+"->"+bo.getBzdlx()+"->"+bo.getYwlx()+"->"+bo.getFylx()+"->");
            System.out.println("报账附件：" + bo.getBzfj());
            System.out.println("------------------");
        }

    }

}